package gui;

import chat.join.Join;
import chat.server.ServerInPut;
import pojo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//登录界面
public class Login extends JFrame{
    private int port;

    public void setPort(int port){
        this.port=port;
    }
    public Login(String name){
        this.setTitle("登录");
        this.setBounds(700,300,400,400);
        Container container = this.getContentPane();

        JPanel panel1 = new JPanel();
        JPanel panel11 = new JPanel();
        JPanel panel12 = new JPanel();
        panel11.add(new JLabel("用户名："));
        JTextField jTextField1 = new JTextField(20);
        jTextField1.setFont(new Font("DIALOG", 1, 17));
        jTextField1.setText(name);
        jTextField1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(jTextField1.getText());
            }
        });
        panel11.add(jTextField1);

        panel12.add(new JLabel("房间号："));
        JTextField jTextField2 = new JTextField(10);
        jTextField2.setFont(new Font("DIALOG", 1, 17));
        jTextField2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(jTextField1.getText());
            }
        });
        panel12.add(jTextField2);

        JPanel jPanelS1 = new JPanel();
        jPanelS1.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton jButton1 = new JButton("加入");
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                //调用join接口，将用户加入服务器
                port=new Join(new User(null,jTextField1.getText(),0)).run();
                //创建线程，用户接收服务器发送的聊天信息
                new Thread(new ServerInPut(port)).start();
                //跳转至聊天主界面
                new Thread(new Chat(new User(null,jTextField1.getText(),0))).start();
            }
        });
        jPanelS1.add(jButton1);


        JPanel jPanelS2 = new JPanel();
        jPanelS2.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton jButton = new JButton("创建新连接");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        jPanelS2.add(jButton);
        jButton.setSize(20,50);

        panel1.setLayout(new GridLayout(8,1));
        panel1.add(new JLabel());
        panel1.add(new JLabel());
        panel1.add(panel11);
        panel1.add(panel12);
        panel1.add(jPanelS1);
        panel1.add(jPanelS2);
        panel1.add(new JLabel());
        panel1.add(new JLabel());


        container.add(panel1);
        container.add(new JPanel(),BorderLayout.EAST);
        container.add(new JPanel(),BorderLayout.WEST);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
